package com.nus.fellowmark.modules.rbac.service.impl;

import com.nus.fellowmark.modules.rbac.dto.UserRoleDTO;
import com.nus.fellowmark.modules.rbac.enums.RoleEnum;
import com.nus.fellowmark.modules.rbac.service.SysUserRoleMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nus.fellowmark.common.utils.PageUtils;
import com.nus.fellowmark.common.utils.Query;

import com.nus.fellowmark.dao.SysUserRoleMappingDao;
import com.nus.fellowmark.entity.SysUserRoleMappingEntity;
import org.springframework.transaction.annotation.Transactional;


@Service("sysUserRoleMappingService")
public class SysUserRoleMappingServiceImpl extends ServiceImpl<SysUserRoleMappingDao, SysUserRoleMappingEntity> implements SysUserRoleMappingService {

    @Autowired
    private SysUserRoleMappingDao sysUserRoleMappingDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysUserRoleMappingEntity> page = this.page(
                new Query<SysUserRoleMappingEntity>().getPage(params),
                new QueryWrapper<SysUserRoleMappingEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Boolean addUserAsRole(Integer userId, RoleEnum roleEnum) {
        SysUserRoleMappingEntity entity = new SysUserRoleMappingEntity();
        entity.setUserId(userId);
        entity.setRoleId(roleEnum.getType());
        int insert = sysUserRoleMappingDao.insert(entity);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean save(UserRoleDTO userRoleDTO) {
        SysUserRoleMappingEntity entity = new SysUserRoleMappingEntity();
        entity.setRoleId(userRoleDTO.getRoleid());
        entity.setUserId(userRoleDTO.getUserId());
        boolean result = this.save(entity);
        if (result) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateById(UserRoleDTO userRoleDTO) {
        SysUserRoleMappingEntity entity = sysUserRoleMappingDao.selectByUserId(userRoleDTO.getUserId());
        entity.setRoleId(userRoleDTO.getRoleid());
        entity.setUserId(userRoleDTO.getUserId());
        boolean result = this.updateById(entity);
        if (result) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeUserbyId(List<Integer> userIds) {
        userIds.forEach(id -> {
            QueryWrapper<SysUserRoleMappingEntity> queryWrapper = new QueryWrapper<SysUserRoleMappingEntity>().eq("userId", id);
            boolean remove = this.remove(queryWrapper);
        });
    }

}
